package shiqi.util;

import lombok.Data;
import shiqi.enums.ExceptionCodeEnum;

/**
 * @author shiqi
 * @version 1.0.0
 * @ClassName Result.java
 * @Description
 * @createTime 2022-05-05
 */
@Data
public class Result<T> {

    private int code;
    private String msg;
    private T t;

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T t) {
        this.code = code;
        this.msg = msg;
        this.t = t;
    }

    public static<T> Result<T> error(String msg){
        return new Result<>(500,msg);
    }

    public static<T> Result<T> error(T t){
        if (t instanceof ExceptionCodeEnum) {
            ExceptionCodeEnum codeEnum = (ExceptionCodeEnum)t;
            return new Result<>(codeEnum.getCode(), codeEnum.getDesc());
        }
        return new Result<>(500,"操作失败");
    }


    public static<T> Result<T> success(T t){
        if (t instanceof ExceptionCodeEnum) {
            ExceptionCodeEnum codeEnum = (ExceptionCodeEnum)t;
            return new Result<>(codeEnum.getCode(), codeEnum.getDesc());
        } else if (t instanceof  String) {
            String successMsg = (String)t;
            return new Result<>(200,successMsg);
        } else {
            return new Result<>(200,"操作成功", t);
        }
    }

}